home *** CD-ROM | disk | FTP | other *** search
/ HTBasic 9.3 / HTBasic 9.3.iso / 93win / data1.cab / DLL_Toolkit / Source / HTBMail / IMapi.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-03-02  |  4.8 KB  |  165 lines

  1. /*************************************************************
  2. HTBmail.dll
  3.  
  4. Copyright 1999 TransEra Corporation
  5.  
  6. htbmail.cpp  
  7.       
  8. With help from http://www.codeguru.com/internet/imapi.shtml 
  9. *************************************************************/
  10. #include "stdafx.h"
  11. #include <mapi.h>
  12. #include "imapi.h"
  13. #include "export.h"
  14.  
  15. HINSTANCE CIMapi::m_hInstMail = (HINSTANCE) NULL;
  16. BOOL      CIMapi::m_isMailAvail = (BOOL) -1;
  17.  
  18. CIMapi::CIMapi()
  19. {
  20.     m_error = 0;                                        //    Initially error free
  21.  
  22.     memset(&m_message, 0, sizeof(MapiMessage));
  23.     memset(&m_from, 0, sizeof(MapiRecipDesc));
  24.     m_message.lpOriginator = &m_from;
  25.     m_from.ulRecipClass = MAPI_ORIG;
  26.  
  27.     if (m_hInstMail == (HINSTANCE) NULL)                //    Load the MAPI dll
  28.         m_hInstMail = ::LoadLibraryA("MAPI32.DLL");
  29.  
  30.     if (m_hInstMail == (HINSTANCE) NULL){
  31.         AfxMessageBox(AFX_IDP_FAILED_MAPI_LOAD);
  32.         m_error = IMAPI_LOADFAILED;
  33.         return;
  34.     }
  35.  
  36.     ASSERT(m_hInstMail != (HINSTANCE) NULL);            //    Now get the pointer to the send function
  37.     (FARPROC&) m_lpfnSendMail = GetProcAddress(m_hInstMail, "MAPISendMail");
  38.  
  39.     if (m_lpfnSendMail == NULL) {
  40.         AfxMessageBox(AFX_IDP_INVALID_MAPI_DLL);
  41.         m_error = IMAPI_INVALIDDLL;
  42.         return;
  43.     }
  44.  
  45.     ASSERT(m_lpfnSendMail != NULL);
  46. }
  47.  
  48. CIMapi::~CIMapi() {
  49.     if (m_hInstMail != (HINSTANCE) NULL)
  50.         ::FreeLibrary(m_hInstMail);
  51.  
  52.     m_hInstMail = (HINSTANCE) NULL;
  53.     
  54.     free(m_message.lpFiles);
  55.     free(m_message.lpRecips);
  56. }
  57.  
  58. BOOL CIMapi::HasEmail() {
  59.     if (m_isMailAvail == (BOOL) -1)
  60.         m_isMailAvail = ::GetProfileInt(_T("MAIL"), _T("MAPI"), 0) != 0 && SearchPath(NULL, _T("MAPI32.DLL"), NULL, 0, NULL, NULL) != 0;
  61.  
  62.     return m_isMailAvail;
  63. }
  64.  
  65. UINT CIMapi::Error() {
  66.     UINT temp = m_error;
  67.  
  68.     m_error = IMAPI_SUCCESS;
  69.     return temp;
  70. }
  71.  
  72. BOOL CIMapi::AllocNewTo() {
  73.     //    Allocate a new MapiRecipDesc structure and initialise it to all zeros
  74.     m_message.lpRecips = (MapiRecipDesc *) realloc(m_message.lpRecips, (m_message.nRecipCount + 1) * sizeof(MapiRecipDesc));
  75.     memset(&m_message.lpRecips[m_message.nRecipCount], 0, sizeof(MapiRecipDesc));
  76.  
  77.     ASSERT(m_message.lpRecips);
  78.     return m_message.lpRecips != (MapiRecipDesc *) NULL;
  79. }
  80.  
  81. BOOL CIMapi::To(LPCTSTR recip) {
  82.     if (AllocNewTo()) {
  83.         //    We succeeded in allocating a new recipient record
  84.         m_message.lpRecips[m_message.nRecipCount].lpszName = (LPTSTR) malloc(strlen(recip) + 1);
  85.         strcpy(m_message.lpRecips[m_message.nRecipCount].lpszName, recip);
  86.         m_message.lpRecips[m_message.nRecipCount].ulRecipClass = MAPI_TO;
  87.         m_message.nRecipCount++;
  88.         return TRUE;
  89.     }
  90.  
  91.     m_error = IMAPI_FAILTO;
  92.     return FALSE;
  93. }
  94.  
  95. BOOL CIMapi::Cc(LPCTSTR recip) {
  96.     if (AllocNewTo()) {
  97.         //    We succeeded in allocating a new recipient record
  98.         m_message.lpRecips[m_message.nRecipCount].lpszName = (LPTSTR) malloc(strlen(recip) + 1);
  99.         strcpy(m_message.lpRecips[m_message.nRecipCount].lpszName, recip);
  100.         m_message.lpRecips[m_message.nRecipCount].ulRecipClass = MAPI_CC;
  101.         m_message.nRecipCount++;
  102.         return TRUE;
  103.     }
  104.  
  105.     m_error = IMAPI_FAILCC;
  106.     return FALSE;
  107. }
  108.  
  109. BOOL CIMapi::Attach(LPCTSTR path, LPCTSTR name) {
  110.     //    Add a new attachment record
  111.     m_message.lpFiles = (MapiFileDesc *) realloc(m_message.lpFiles, (m_message.nFileCount + 1) * sizeof(MapiFileDesc));
  112.     memset(&m_message.lpFiles[m_message.nFileCount], 0, sizeof(MapiFileDesc));
  113.  
  114.     ASSERT(m_message.lpFiles);
  115.     
  116.     if (m_message.lpFiles == (MapiFileDesc *) NULL) {
  117.         m_error = IMAPI_FAILATTACH;
  118.         return FALSE;
  119.     }
  120.  
  121.     m_message.lpFiles[m_message.nFileCount].lpszPathName = (LPTSTR) malloc(strlen(path) + 1);
  122.     strcpy(m_message.lpFiles[m_message.nFileCount].lpszPathName, path);
  123.  
  124.     if (name != (LPCTSTR) NULL) {
  125.         m_message.lpFiles[m_message.nFileCount].lpszFileName = (LPTSTR) malloc(strlen(name) + 1);
  126.         strcpy(m_message.lpFiles[m_message.nFileCount].lpszFileName, name);
  127.     }
  128.  
  129.     m_message.nFileCount++;
  130.     return TRUE;
  131. }
  132.  
  133. BOOL CIMapi::Send(ULONG flags) {
  134.     CWaitCursor wait;
  135.     int            offset = m_text.GetLength();
  136.  
  137.     //    Add 1 space per attachment at the end of the body text.
  138.     m_text += CString(' ', m_message.nFileCount);
  139.  
  140.     //    Set each attachment to replace one of the added spaces at the end of the body text.
  141.     for (UINT i = 0; i < m_message.nFileCount; i++)
  142.         m_message.lpFiles[i].nPosition = offset++;
  143.  
  144.     m_message.lpszNoteText = (LPTSTR) (LPCTSTR) m_text;    //  Set the body text
  145.  
  146.     int nError = m_lpfnSendMail(0, (ULONG) g_hBasicWindow, &m_message, MAPI_LOGON_UI | flags, 0);
  147.  
  148.     //    Now free malloced recipients
  149.     for (i = 0; i < m_message.nRecipCount; i++)
  150.         free(m_message.lpRecips[i].lpszName);
  151.  
  152.     //    Then free malloced attachments
  153.     for (i = 0; i < m_message.nFileCount; i++) {
  154.         free(m_message.lpFiles[i].lpszPathName);
  155.         free(m_message.lpFiles[i].lpszFileName);
  156.     }
  157.  
  158.     if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE) {
  159.         AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);
  160.         return FALSE;
  161.     }
  162.  
  163.     return TRUE;
  164. }
  165.